package com.lanyou.esb.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.esb.datadictionary.EnableType;


@Entity
@EntityListeners({ AuditingEntityListener.class})
@Table(name = "T_ESB_BU_IF_FTP_SERVER")//还没有增加监听
public class FtpServer {

	private String id;//FTP服务器信息表ID
	
	private String host;//主机
	
	private String port;//端口
	
	private String userName;//用户名
	
	private String password;//密码
	
	private String fileDir;//文件路径
	
	private String ifCover;//是否覆盖目标文件
	
	private String ifDelSource;//是否删除源文件
	
	private String ifAnonymous;//是否匿名
	
	private String hostType;//主机类型
	
	private String fileName;//文件名称
	
	private String serverCode;//FTP服务器编码
	
	private Ftp ftp;
	
	private FieldSystem ownFieldSystem;
	
	private String creator;//创建人

	private String modifier;//更新人员

	private Date createTime;//创建时间

	private Date modifyTime;//更新时间
	
	private String passiveMode;//是否启动被动模式0：否；1：是
	
	
	
	public final static Specification<FtpServer> EmptySpecification = new Specification<FtpServer>(){
		@Override
		public Predicate toPredicate(Root<FtpServer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<FtpServer> idSpecification(String id){
		return new Specification<FtpServer>(){
			@Override
			public Predicate toPredicate(Root<FtpServer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "IF_FTP_SERVER_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name="HOST")
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	@Column(name="PORT")
	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	@Column(name="USER_NAME")
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Column(name="PASSWORD")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name="FILE_DIR")
	public String getFileDir() {
		return fileDir;
	}

	public void setFileDir(String fileDir) {
		this.fileDir = fileDir;
	}

	
	@Column(name="IS_COVER")
	public String getIfCover() {
		return ifCover;
	}

	public void setIfCover(String ifCover) {
		this.ifCover = ifCover;
	}

	@Column(name="IS_DEL_SOURCE")
	public String getIfDelSource() {
		return ifDelSource;
	}

	public void setIfDelSource(String ifDelSource) {
		this.ifDelSource = ifDelSource;
	}
	
	@Column(name="IS_ANONYMOUS")
	public String getIfAnonymous() {
		return ifAnonymous;
	}



	public void setIfAnonymous(String ifAnonymous) {
		this.ifAnonymous = ifAnonymous;
	}

	
	@Column(name="HOST_TYPE")
	public String getHostType() {
		return hostType;
	}



	public void setHostType(String hostType) {
		this.hostType = hostType;
	}


	@Column(name="FILE_NAME")
	public String getFileName() {
		return fileName;
	}


	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	@Column(name="CONTROL_ENCODING")
	public String getServerCode() {
		return serverCode;
	}


	public void setServerCode(String serverCode) {
		this.serverCode = serverCode;
	}


	@Column(name="PASSIVE_MODE")
	public String getPassiveMode() {
		return passiveMode;
	}



	public void setPassiveMode(String passiveMode) {
		this.passiveMode = passiveMode;
	}



	@ManyToOne
	@JoinColumn(name = "IF_FTP_ID")
	public Ftp getFtp() {
		return ftp;
	}

	public void setFtp(Ftp ftp) {
		this.ftp = ftp;
	}

	
	@ManyToOne
	@JoinColumn(name = "FIELD_SYSTEM_ID")
	public FieldSystem getOwnFieldSystem() {
		return ownFieldSystem;
	}


	public void setOwnFieldSystem(FieldSystem ownFieldSystem) {
		this.ownFieldSystem = ownFieldSystem;
	}



	@CreatedBy
	@Column(length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	@Transient
	public String getSystemCode() {
		return ownFieldSystem.getCode();
	}


	@Transient
	public String getIsEnable(){
		return EnableType.ENABLE.ValueCode;
	}
	
	
	@Transient
	public String getCreateTimeForExport(){
		if(createTime != null){
			return createTime.toString().substring(0, createTime.toString().length() - 2);
		}else{
			return "";
		}
		
	}
	
	@Transient
	public String getModifyTimeForExport(){
		if(modifyTime != null){
			return modifyTime.toString().substring(0, modifyTime.toString().length() - 2);
		}else{
			return "";
		}
	}
}
